// 获取用户详情
async function getUserDetailsRequest() {
    const userInfo = JSON.parse(localStorage.getItem('userInfo'))
    const res = await $.ajax({
        url: 'http://localhost:8081/api/getUserDetails.php',
        type: 'get',
        data: {
            user_id: userInfo.id
        }
    })
    if (res.code === 1) {
        init(res)
    }
}
getUserDetailsRequest()

// 渲染头像
function renderAvatar(avatar) {
    const pic = document.querySelector('.header .right .user .pic')
    pic.style.backgroundImage = `url(${avatar !== '' ? avatar : '../../assets/defaultAvatar.png'})`
}

// 初始化
function init(res) {
    // 设置用户名
    $('.header .user .name').text(res.data.username)

    // 渲染头像
    renderAvatar(res.data.avatar)
}

// 退出
$('.header .right .logout').click(function () {
    console.log(1)
    localStorage.clear()
    location.href = '../../view/login/login.php'
})